package com.rran.study.designpattern.behavioral.strategy;

import com.rran.study.designpattern.behavioral.strategy.enums.PayEnums;
import com.rran.study.designpattern.behavioral.strategy.factory.PayStrategySpringFactory;
import com.rran.study.designpattern.behavioral.strategy.model.PayContext;
import com.rran.study.designpattern.behavioral.strategy.strategy.PayStrategy;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

import java.math.BigDecimal;

/**
 * 策略模式基于Spring框架
 * 相比于枚举反射工厂获得策略类更优雅
 * 把策略类给spring容器管理 不需要new策略类的实例对象 推荐
 */
@EnableAutoConfiguration
public class StrategySpringApplication {

    public static void main(String[] args) {

        //非web方式启动
        ConfigurableApplicationContext context = new SpringApplicationBuilder(StrategySpringApplication.class)
                .web(WebApplicationType.NONE)
                .run(args);

        PayContext payContext = new PayContext();
        payContext.setName("Rran");
        payContext.setPrice(new BigDecimal(200));
        payContext.setStrategy(PayEnums.UNION);

        PayStrategy payStrategy = PayStrategySpringFactory.getPayStrategy(payContext.getStrategy().name());
        payStrategy.pay(payContext);

        //关闭上下文
        context.close();

    }
}
